import 'package:ladder_cash/app/app.dart';
import 'package:ladder_cash/app/key_mix.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_box.dart';
import 'package:liuxlcommon/view/my_style.dart';
import 'package:ladder_cash/app/com/service/address_service.dart';
import 'package:ladder_cash/app/com/view/common_view.dart';
import 'package:ladder_cash/generated/l10n.dart';
import 'package:ladder_cash/value/app_colors.dart';

import '../../../generated/assets.dart';

class AddressSelectDialog {
  show({title, required call(address)}) async {
    List<dynamic>? addressData = await AddressService().getAddressData();
    showAddressDialog3(addressData!, title: title, call: call);
    return;
  }

  Widget dialogCityItem(item, bool check, onTap) => LRow(
        lStyle: LStyle(height: 43.5.w),
        onTap: onTap,
        children: [
          LText(item['oneName'.hx]??item['name'.hx],
              lStyle: LStyle(
                  color: (item['check'] ?? false)
                      ? AppColors.primaryColor
                      : AppColors.textColor,
                  flex: 1)),
          // if (check) LImage.easy(Assets.iconListIconGo, 24.w, 24.w)
        ],
      );

  showAddressDialog3(List<dynamic> _addressData,
      {title, required call(address)}) {

    String city = App.localeEn?'Select':'Seleccionar' ;
    String area = city;
    String street = city;

    int selectIndex = 0; //0 是城市 2 街道

    late List<dynamic> addressData;
    addressData = _addressData;

    List list = [];
    list.addAll(addressData);

    CommonView
        .bottomDialog( height: 504.w, title: title, confirm: () {}, children: [
      GetBuilder<AddressLogic>(
        init: AddressLogic(),
          builder: (logic) => LColumn(
                  lStyle: LStyle(
                      height: 325.w, padding: EdgeInsets.only(left: 24.w)),
                  children: [
                    24.lsh,
                    [
                      LExpand(
                        child: LText(city,
                            lStyle: selectIndex > 0 ? selectedSty : selectSty),
                      )
                    ].row,
                    if (selectIndex > 0) ...[
                      4.lsh,
                      [
                        LText(area,
                            lStyle: selectIndex > 1 ? selectedSty : selectSty),
                        8.lsw,
                        if (selectIndex == 2) LText(street, lStyle: selectSty),
                        LExpand()
                      ].row,
                    ],
                    8.lsh,
                    LDivider(1.w,AppColors.textSecondColor).marginOnly(right: 20.w),
                    <Widget>[
                      for (var item in list)
                        dialogCityItem(item, false, () {
                          if (selectIndex == 0) {
                            city = item['oneName'.hx];
                          } else if (selectIndex == 1) {
                            area = item['name'.hx];
                            if (item['dictProvinceList'.hx] == null) {
                              call("$city|$area");
                              Get.back();
                            }
                          } else {
                            street = item['name'.hx];
                            call("$city|$area|$street");
                            Get.back();
                          }
                          selectIndex++;
                          // list = item['listData'.hx];
                          list = item['dictProvinceList'.hx];
                          logic.update();
                        })
                    ].columnSlidExpand
                  ]))
    ]);
  }

  get dialog3Sty => LStyle(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      width: double.infinity,
      height: 432.w,
      margin: EdgeInsets.symmetric(horizontal: 24.w),
      radius: 16.w,
      backgroundColor: Colors.white);

  get selectedSty => LStyle(fontSize: 15.sp, color:AppColors.textColor);

  get selectSty => LStyle(fontSize: 16.sp, color: AppColors.secondColor);
}

class AddressLogic extends GetxController {}
